package com.lenovo.smarttraffic.ui.fragment.fragmentAdapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.ui.fragment.fgBean.AdapterBean;

public class Fg1Adapter extends BaseAdapter {
    String TAG = "Fg1Adapter";

    private List<AdapterBean> objects = new ArrayList<AdapterBean>();

    private Context context;
    private LayoutInflater layoutInflater;

    public Fg1Adapter(Context context, List<AdapterBean> list) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.objects = list;
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public AdapterBean getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.zm_fg_item, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((AdapterBean) getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(AdapterBean object, ViewHolder holder) {
        //TODO implement
        holder.tvTime.setText(object.getTime());
        holder.tvDescribe.setText(object.getContent());
        holder.tvTitle.setText(object.getTiele());
        if (object.getImg().equals("w")) {
            holder.ivImg.setVisibility(View.INVISIBLE);
        } else {
            Log.e(TAG, "initializeViews: " + object.getImg());
            holder.ivImg.setVisibility(View.VISIBLE);
            Glide.with(context).load("http://192.168.1.110:8088/transportservice/images/spot/gugong.png").into(holder.ivImg);
        }
    }

    protected class ViewHolder {
        private TextView tvTitle;
        private TextView tvTime;
        private TextView tvDescribe;
        private ImageView ivImg;

        public ViewHolder(View view) {
            tvTitle = (TextView) view.findViewById(R.id.tv_title);
            tvTime = (TextView) view.findViewById(R.id.tv_time);
            tvDescribe = (TextView) view.findViewById(R.id.tv_describe);
            ivImg = (ImageView) view.findViewById(R.id.iv_img);
        }
    }
}
